/**
 * Created by zxh on 14-7-14.
 */

ms.UISummary = ms.UILayer.extend({
    //_startPoint: null,
    _isSubmit: null,
    _array: null,
    ctor: function() {
        this._super();
        this._ccsFileName = "summary.json";
        this._array = {};
    },

    onEnter: function() {
        this._super();
        this._submitButton.setVisible(false);
        var img;
        for (var i = 1 ; i <=4; i++) {
            img = this["_img" + i];
            img.addTouchEventListener(this.onImageEvent, this);
            img.setTouchEnabled(true);
            img.originPos = img.getPosition();
            this["_panel" + i].setVisible(false);
        }
    },

    getSelectPanel: function(pos) {
        var panel;
        for (var i = 1; i <=4 ;i++) {
            panel = this["_panel" + i];
            var rect = panel.getBoundingBox();
            if (cc.rectContainsPoint(rect, pos)) {
                panel.runAction(cc.RepeatForever.create(cc.Blink.create(1, 5)));
            } else {
                panel.stopAllActions();
            }
        }
    },

    onImageEvent: function(sender, type) {
        //cc.log(type);
        if (this._isSubmit) {
            return;
        }

        if (type === 0) {
            for (var i = 1; i <= 4; i++ ) {
                if (this._array[i] === sender) {
                    this._array[i] = null;
                }
            }
            return true;
        } else if (type === 1) {

            sender.setPosition( sender.getTouchMovePos());
            this.getSelectPanel(sender.getTouchMovePos());
//            if (panel) {
//                panel.runAction(cc.RepeatForever.create(cc.Blink.create(0.1, 2)));
//            }

        } else if (type === 2) {

            var panel;
            var isOk;
            for (var i = 1; i <=4 ;i++) {

                panel = this["_panel" + i];
                panel.stopAllActions();
                var rect = panel.getBoundingBox();
                var pos = sender.getTouchEndPos();
                if (cc.rectContainsPoint(rect, pos)) {
                    pos = panel.getPosition();
                    pos.x += panel.width * 0.5;
                    pos.y += panel.height * 0.5;
                    sender.setPosition(pos);
                    isOk = true;
                    this._array[i] = sender;
                    break;
                }
            }

            if (!isOk) {
                sender.setPosition(sender.originPos);
            }

            this.checkImg();
        }
    },

    checkImg: function() {

        var image;
        for (var i = 1; i <= 4; i++) {
            image = this._array[i];
            if (!image) {
                this._submitButton.setVisible(false);
                return;
            }
        }
        this._submitButton.setVisible(true);
        return true;
    },

    onSubmitButtonTouchEnded: function(sender) {
        if (!sender.isVisible()) {
            return;
        }

        var img, panel;
        for (var i = 1; i <= 4; i++) {
            img = this._array[i];

            var flagImg = ccui.ImageView.create();
            if (img.tag === i) {
                flagImg.loadTexture("res/ccs/flag0002.png");
            } else {
                flagImg.loadTexture("res/ccs/flag0001.png");
            }

            img.addChild(flagImg);
        }
        this._isSubmit = true;
    }


});